package org.jeecg.modules.app.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.login.service.ProgramLoginService;
import org.jeecg.modules.power.user.entity.YlToken;
import org.jeecg.modules.power.user.entity.YlUser;
import org.jeecg.modules.power.user.mapper.YlTokenMapper;
import org.jeecg.modules.power.user.service.IYlTokenService;
import org.jeecg.modules.power.user.service.IYlUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: jeecg-boot-parent
 * @BelongsPackage: org.jeecg.modules.app.login.service.impl
 * @Author: mi
 * @CreateTime: 2024-07-10 18:02
 * @Description:
 */
@Service
public class ProgramLoginServiceImpl implements ProgramLoginService {
    @Autowired
    private IYlUserService ylUserService;//用户表


    @Autowired
    private YlTokenMapper ylTokenMapper;//token表



    @Override
    @Transactional
    public Result<?> addYlUser(YlUser ylUser) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date day = new Date();
        String token ="";//用户token
        String userId ="";//用户id
        if (ylUser.getSource()==0){//微信
            YlUser user=  ylUserService.getOne(new LambdaQueryWrapper<YlUser>().eq(YlUser::getUnionid,ylUser.getUnionid()));
            if (oConvertUtils.isEmpty(user)){
                //如果没有 查询 此手机号 有没有绑定支付宝  如果有和支付宝 账号绑定 一个账号
                String tel=  ylUser.getTel();
                List<YlUser>userList= ylUserService.list(new LambdaQueryWrapper<YlUser>().eq(YlUser::getTel,tel));
                if (userList.size()>0){
                    //合并账号
                    YlUser editUser=new YlUser();
                    editUser.setId(userList.get(0).getId());
                    editUser.setUnionid(ylUser.getUnionid());
                    editUser.setOpenid(ylUser.getOpenid());
                    ylUserService.updateById(editUser);
                    userId =editUser.getId();
                }else {
                    ylUser.setStatus(1);
                    //没有 新增用户
                    ylUserService.save(ylUser);
                    userId =ylUser.getId();
                }
            }else {
                //授权成功直接登陆
                userId =user.getId();
            }
            //用户 生成token
            token = 	JwtUtil.sign(userId,ylUser.getUnionid());
        }else {//支付宝
            YlUser user=  ylUserService.getOne(new LambdaQueryWrapper<YlUser>().eq(YlUser::getAliOpenid,ylUser.getAliOpenid()));
            if (oConvertUtils.isEmpty(user)){
                //如果没有 查询 此手机号 有没有绑定微信  如果有和微信 账号绑定 一个账号
                String tel=  ylUser.getTel();
                List<YlUser>userList= ylUserService.list(new LambdaQueryWrapper<YlUser>().eq(YlUser::getTel,tel));
                if (userList.size()>0){
                    //合并账号
                    YlUser editAliUser=new YlUser();
                    editAliUser.setId(userList.get(0).getId());
                    editAliUser.setAliUserId(userList.get(0).getAliUserId());
                    editAliUser.setAliOpenid(userList.get(0).getAliOpenid());
                    ylUserService.updateById(editAliUser);
                    userId =editAliUser.getId();
                }else {
                    ylUser.setStatus(1);
                    //没有 新增用户
                    ylUserService.save(ylUser);
                    userId =ylUser.getId();
                }
            }else {
                //授权成功直接登陆
                userId =user.getId();
            }
            //用户 生成token
            token = 	JwtUtil.sign(userId,ylUser.getAliOpenid());
        }
        ylTokenMapper.delGasTokenByUserId(userId);//清除其他token  根据用户id
        YlToken ylToken=new YlToken();
        ylToken.setToken(token);
        ylToken.setUserId(userId);
        ylToken.setUpdateTime(day);
        ylToken.setType(0);//小程序段
        ylToken.setExpireTime(  DateUtils.addDaysToDate(day,15));//过期时间增加七天
        ylTokenMapper.insert(ylToken);
        map.put("token",token);
        return Result.OK(map);
    }


    @Override
    public Result<?> userQuit(String token) {
        ylTokenMapper.delGasToken(token);
        return Result.OK("退出成功");
    }
}
